package com.spzx.product.receiver;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.spzx.product.service.ISkuStockService;
import com.spzx.rabbit.constant.MqConst;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : 戚欣扬
 * @Description :
 */
@Slf4j
@Component
public class ProductReceiver
{
    @Autowired
    private ISkuStockService skuStockService;

    /**
     * 解锁库存
     * @param orderNo 订单号
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_UNLOCK, durable = "true"),
            key = {MqConst.ROUTING_UNLOCK}
    ))
    public void unlock(String orderNo, Message message, Channel channel){
        //业务处理
        log.info("[商品服务]监听解锁库存消息：{}", orderNo);
        skuStockService.unlock(orderNo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}


